package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.ProjectOrderAdapter;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.base.RecycleViewDivider;
import com.wnhz.work.yz.bean.ProjectChild1;
import com.wnhz.work.yz.molder.SpecialShopMolder;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 选择商品特批信息
 */
public class SpecialShopListActivity extends BaseActivity implements View.OnClickListener, BaseRVAdapter.OnItemClickListener {

    private static final String TAG = "SpecialShopListActivity";
    private BaseRVAdapter<SpecialShopMolder> adapter;
    private ArrayList<SpecialShopMolder> mList = new ArrayList<>();
    private RecyclerView recyclerView;
    private ListView my_listView;
    private ArrayList<Integer> ints;
    //  private List<FitmentBean> beanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special_shop);
        initData();
        upload();
        initTitle();
        ints = getIntent().getIntegerArrayListExtra("ints");
//        for (int i = 0; i < 9; i++) {
//            SpecialShopMolder molder = new SpecialShopMolder();
//            molder.setId(i);
//            mList.add(molder);
//        }


    }

    private void initRecycleView(ArrayList<SpecialShopMolder> mList) {
        Log.e(TAG, "-----" + mList.size());
        for (int i = 0; i < mList.size(); i++) {
            for (Integer anInt : ints) {
                if (anInt == mList.get(i).getId()) {
                    Log.e(TAG, "onCreate: " + i);
                    mList.get(i).setSelect(true);
                    continue;
                }
            }
        }

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        adapter = new BaseRVAdapter<SpecialShopMolder>(this, mList) {

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.project_list_item;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                TextView tv_sale = holder.getTextView(R.id.tv_sale);
                tv_sale.setVisibility(View.VISIBLE);
                SpecialShopMolder bean = mList.get(position);
                if (bean.isSelect()) {
                    tv_sale.setText("已选择");
                    tv_sale.setBackgroundColor(getResources().getColor(R.color.white));
                } else {
                    tv_sale.setText("请选择");
                    tv_sale.setBackgroundDrawable(getResources().getDrawable(R.drawable.go_pay));
                }
                x.image().bind(holder.getImageView(R.id.iv_img), bean.getIv_img());
                holder.setTextView(R.id.tv_title, bean.getTitle_tv());
                holder.setTextView(R.id.tv_numbers, "×" + bean.getNumber_tv());
                holder.setTextView(R.id.tv_type, bean.getMaterial());
                holder.setTextView(R.id.tv_price, bean.getTv_price());
            }
        };


        adapter.addItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayout.HORIZONTAL, R.drawable.line_shape));
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        proojectList = (List<ProjectChild1>) getIntent().getSerializableExtra("dataList");
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(R.string.special_shop_list_title);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.tv_right).setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }

    @Override
    public void itemSelect(int position) {
        if (mList.get(position).isSelect()) {
            MyToast("这件物品已经被选择了");
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("molder", mList.get(position));
        setResult(RESULT_OK, intent);
        finish();
    }

    //选中的下单列表
    private List<ProjectChild1> proojectList;

    private ProjectOrderAdapter mAdapter;

    private void upload() {
        Map<String, Object> map = new HashMap<>();
        StringBuilder builder = new StringBuilder("-");
        for (int i = 0; i < proojectList.size(); i++) {
            builder.append(proojectList.get(i).getCart_id());
            builder.append("-");
        }
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("cartIds", builder);
        XUtil.Post(Url.ORDER_BUDGETBUYPAGE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:ORDER_BUDGETBUYPAGE= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    if ("1".equals(res)) {
                        JSONObject info = jsonObject.optJSONObject("info");
                        JSONArray array = info.optJSONArray("data");
                        Log.i(TAG, array.length() + "" + array.toString());
                        if (array != null && array.length() > 0) {
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject array1 = array.optJSONObject(i);
                                if (array1 != null) {
                                    /**
                                     *          "package_id": "173",
                                     "cart_id": "88",
                                     "room_number": "sdsf",
                                     "package_name": "\u5c45\u9038\u00b70816",
                                     "package_money": "5990.00",
                                     "package_cate": "\u6574\u4f53\u6d74\u5ba4",
                                     "img": "http:\/\/youzhu.unohacha.com\/Uploads\/Picture\/uploads\/20170113\/587869040c4c5.jpg",
                                     "package_sku_info": "\u5e26\u7ba1\u4e95,AL-\u957f\u8fb9\u5de6\u5f00\u95e8",
                                     "nums": "5"
                                     */
                                    String package_id = array1.optString("package_id");
                                    String cart_id = array1.optString("cart_id");
                                    String room_number = array1.optString("room_number");
                                    String package_name = array1.optString("package_name");
                                    String package_money = array1.optString("package_money");
                                    String package_cate = array1.optString("package_cate");
                                    String img = array1.optString("img");
                                    String package_sku_info = array1.optString("package_sku_info");
                                    String nums = array1.optString("nums");
                                    //int id, String title_tv, String material, String tv_price, String number_tv, String iv_img
                                    SpecialShopMolder molder = new SpecialShopMolder(Integer.parseInt(cart_id), package_name, package_sku_info, package_money, nums, img);
                                    molder.setPackage_id(package_id);
                                    mList.add(molder);
                                    // Log.i(TAG,"--------"+package_sku_info);
                                }
                            }
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

//                mAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFinished() {
                super.onFinished();
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacks(null);
        super.onDestroy();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            initRecycleView(mList);
        }
    };
}
